﻿@inject IHttpService _httpService

@inject NavigationManager NavigationManager
@inject IPopupService PopupService


<div style="display: flex; justify-content: center;">
    @if (Model.Type == LotteryType.Manual && Model.IsEnd == false)
    {
        <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="mdi-wallet-giftcard " Tooltip="抽奖" OnClick="OnDraw" Color="info" Class="me-3"/>
    }

    <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="mdi-pencil" Tooltip="编辑" OnClick="OnEdit" Color="success"  Class="me-3"/>
    @if (Model.IsHidden)
    {
        <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="mdi-eye" Tooltip="显示" OnClick="@(()=>OnClickLottery(false))" Color="info" />

    }
    else
    {
        <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="mdi-eye-off" Tooltip="隐藏" OnClick="@(()=>OnClickLottery(true))" Color="error"  />
    }
</div>

@code {
    [Parameter]
    public LotteryViewModel Model { get; set; }
    [Parameter]
    public EventCallback StateChanged { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public async Task OnClickLottery(bool isHidden)
    {
        string text = isHidden ? "隐藏" : "显示";

        try
        {
            var obj = await _httpService.PostAsync<HiddenLotteryModel, Result>("api/lotteries/HiddenLottery", new HiddenLotteryModel { Ids = new long[] { Model.Id }, IsHidden = isHidden });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync(text + "抽奖失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync(text + "抽奖成功", text + "抽奖成功");
                await StateChanged.InvokeAsync();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, text + "抽奖失败");
        }
    }

    public void OnEdit()
    {
        NavigationManager.NavigateTo( "/lotteries/editlottery/" + Model.Id);
        
    }
    public void OnDraw()
    {
        NavigationManager.NavigateTo( "/lotteries/drawlottery/" + Model.Id);
        
    }
}
